﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormImportOutStoreBill : BaseForm
    {
        public FormImportOutStoreBill()
        {
            InitializeComponent();
            this.gridView1.FocusedRowChanged += GridView1FocusedRowChanged;
            if (GlobalClass.RunTimeMode)
            {
                UI.DataBinding.Binding.BindOutStoreTypeInConfig(this.qOutStoreBill1.LookUpEditOutStoreType);
            }
        }

        private void GridView1FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            ShowDetail(e.FocusedRowHandle);
        }

        private void ShowDetail(int rowHandler)
        {
            try
            {
                if (rowHandler < 0)
                    return;
                var outStoreBill = (OutStoreBill) this.gridView1.GetRow(rowHandler);
                if (outStoreBill != null)
                {
                    var client = Factory.WmsServiceFactory.GetServiceClient();
                    var list = client.GetOutStoreBillDtlsByCondition(string.Format(" and BillID = '{0}'",outStoreBill.BillID));
                    this.gridControlOutBillDtl.DataSource = list;
                }
            }
            catch (Exception exception)
            {
                Exceptions.ExceptionHandler.ShowException(exception);
            }
        }

        public OutStoreBill POutStoreBill
        {
            get; private set;
        }

        private void RepositoryItemButtonEditOkButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var entity = (OutStoreBill)this.gridView1.GetRow(this.gridView1.FocusedRowHandle);
            this.POutStoreBill = entity;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            var condition = this.qOutStoreBill1.GetCondition();
            var list = Factory.WmsServiceFactory.GetServiceClient().GetAbnormalOutStoreBill(condition);
            this.gridControlOutStoreBill.DataSource = list;
            if(list.Count>0)
            {
                ShowDetail(this.gridView1.FocusedRowHandle);
            }
        }
    }
}
